package me.yan.controller.admin;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import me.yan.controller.BaseController;
import me.yan.dto.StatisticsDto;
import me.yan.pojo.CommentDomain;
import me.yan.pojo.ContentDomain;
import me.yan.pojo.LogDomain;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Tag(name="后台首页")
@Controller
@RequestMapping("/admin")
@Slf4j
public class IndexController extends BaseController {

    private static final Logger LOGGER=log;

    @Operation(summary = "进入首页")
    @GetMapping(value = {"","/index"})
    public String index(HttpServletRequest request, Model model) {
        LOGGER.info("Enter admin index method");
        List<CommentDomain> comments = siteService.getComments(5);  //获取评论
        List<ContentDomain> contents = siteService.getNewArticles(5);  //获取文章
        StatisticsDto statistics = siteService.getStatistics(); //获取后台数据
        // 取最新的20条日志
        PageInfo<LogDomain> logs = logService.getLogs(1, 5);
        List<LogDomain> list = logs.getList();
        request.setAttribute("comments", comments);
        request.setAttribute("articles", contents);
        request.setAttribute("statistics", statistics);
        request.setAttribute("logs", list);
        LOGGER.info("Exit admin index method");
        model.addAttribute("commons", commons);
        return "admin/index";
    }
}
